package com.example.himalaya.fragments;

import static com.example.himalaya.base.BaseApplication.getAppContext;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.DetailActivity;
import com.example.himalaya.R;
import com.example.himalaya.adapters.AlbumListRvAdapter;
import com.example.himalaya.base.BaseFragment;
import com.example.himalaya.interfaces.ISubscriptionViewCallback;
import com.example.himalaya.presenters.AlbumDetailPresenter;
import com.example.himalaya.presenters.SubPresenter;
import com.example.himalaya.utils.Constants;
import com.example.himalaya.views.ConfirmDialog;
import com.example.himalaya.views.UILoader;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

/**
 * 订阅Fragment片段
 */
public class SubscriptionFragment extends BaseFragment implements ISubscriptionViewCallback {

    private UILoader mUiLoader;
    private SubPresenter mSubPresenter;
    private AlbumListRvAdapter mAdapter;
    private RecyclerView mSubListRv;
    private TwinklingRefreshLayout mRefreshLayout;

    @Override
    protected View onSubViewLoaded(LayoutInflater inflater, ViewGroup container) {
        //view加载完成
        FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_subscription, container, false);


        if (mUiLoader==null) {

            mUiLoader = new UILoader(getAppContext()) {
                @Override
                protected View getSuccessView(ViewGroup container) {
                    return createSussessView(container);
                }


                @Override
                protected View getDataEmptyView() {
                    View emptyView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty_view, this, false);
                    TextView emptyViewText = emptyView.findViewById(R.id.empty_view_text);
                    emptyViewText.setText("没有订阅专辑呢，赶紧去订阅吧!");
                    return emptyView;
                }
            };
        }else {
            if(mUiLoader.getParent() instanceof ViewGroup) {
                ((ViewGroup) mUiLoader.getParent()).removeView(mUiLoader);
            }
        }




        mSubPresenter = SubPresenter.getSubPresenter();
        mSubPresenter.registerViewCallBack(this);
        mSubPresenter.getSubList();

        view.addView(mUiLoader);

        //返回view给父类Fragment片段
        return view;
    }

    private View createSussessView(ViewGroup container) {
        View successView = LayoutInflater.from(container.getContext()).inflate(R.layout.fragment_subscription_item, container, false);
        mSubListRv = successView.findViewById(R.id.subscription_list);
        mSubListRv.setLayoutManager(new LinearLayoutManager(container.getContext()));
        mSubListRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.top = UIUtil.dip2px(view.getContext(),2);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 2);
                outRect.left=UIUtil.dip2px(view.getContext(),5);
                outRect.right=UIUtil.dip2px(view.getContext(),5);
            }
        });
        mAdapter = new AlbumListRvAdapter();
        mAdapter.setOnItemClickListener(new AlbumListRvAdapter.onItemClickListener() {
            @Override
            public void OnItemClick(int position, Album album) {
                AlbumDetailPresenter albumDetailPresenter = AlbumDetailPresenter.getAlbumDetailPresenter();
                albumDetailPresenter.setTargetAlbum(album);

                //跳转到专辑详情界面
                Intent intent = new Intent(getContext(), DetailActivity.class);
                startActivity(intent);
            }
        });
        mAdapter.setOnAlbumItemLongClickListener(new AlbumListRvAdapter.OnAlbumItemLongClickListener() {
            @Override
            public void onAlbumItemLongClick(int position, Album album) {
                ConfirmDialog confirmDialog = new ConfirmDialog(getActivity());
                confirmDialog.setOnDialogActionClickListener(new ConfirmDialog.OnDialogActionClickListener() {
                    @Override
                    public void onCancelSubClick() {
                        if (mSubPresenter != null&&album!=null) {
                            mSubPresenter.deleteSub(album);
                        }
                    }

                    @Override
                    public void onGiveUpClick() {

                    }
                });
                confirmDialog.show();
            }
        });
        mSubListRv.setAdapter(mAdapter);
        mRefreshLayout = successView.findViewById(R.id.recommend_refreshLayout);
        mRefreshLayout.setPureScrollModeOn(true);
        return successView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSubPresenter != null) {
            mSubPresenter.unRegisterViewCallBack(this);
            mSubPresenter = null;
        }

    }

    @Override
    public void onAddSub(boolean isSuccess) {
        Toast.makeText(getContext(),isSuccess?"订阅成功":"订阅失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDeleteSub(boolean isSuccess) {
        Toast.makeText(getContext(),isSuccess?"取消订阅成功":"取消订阅失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onGetSubList(List<Album> albums) {
        if (albums==null||albums.size()==0) {
            mUiLoader.updateUIStatus(UILoader.UIStatus.EMPTY);
        }else {
            //更新数据
            mAdapter.setData(albums);
            mUiLoader.updateUIStatus(UILoader.UIStatus.SUCCESS);
        }
    }

    @Override
    public void onSubFull() {
        Toast.makeText(getContext(),"订阅数量不可以超过"+ Constants.MAX_SUB_COUNT,Toast.LENGTH_SHORT).show();
    }


}
